/*
 * @Author: szx
 * @Date: 2022-04-12 13:07:01
 * @LastEditTime: 2022-04-12 13:11:13
 * @Description:
 * @FilePath: \leetcode\剑指offerII\051\051.js
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxPathSum = function (root) {
    const res = Number.MIN_SAFE_INTEGER;
    const dfs = (root) => {
        if (!root) return 0;
        const left = dfs(root.left);
        const right = dfs(root.right);
        const max = Math.max(left + root.val, right + root.val);
        if (max > res) res = max;
        return max;
    };
    return dfs(root);
};
